<?php declare(strict_types=1);


namespace PinWuYun\domain\helper;

use Exception;
use GuzzleHttp\Exception\GuzzleException;
use PinWuYun\domain\dao\GoodsReleaseDao;
use PinWuYun\domain\data\YouZanData\GoodsData;
use PinWuYun\domain\service\commonServer\SyncService;
use PinWuYun\domain\service\starLinkServer\Config;
use PinWuYun\domain\service\starLinkServer\Goods;
use PinWuYun\domain\service\starLinkServer\StarDataServer;
use PinWuYun\domain\service\youZanServer\GoodsServer;

/**
 * Class GoodsUpdateHelper
 * @package PinWuYun\domain\helper
 */
class GoodsUpdateHelper
{
    /** 加载需要的类 */
    private $YouZanData;
    private $StarGoodsServer;
    private $YouZanService;
    private $SyncService;
    private $GoodsReleaseDao;
    private $StarDataServer;
    public function __construct()
    {
        $this->YouZanData = new GoodsData();
        $this->StarGoodsServer = new Goods();
        $this->YouZanService = new GoodsServer();
        $this->SyncService = new SyncService();
        $this->GoodsReleaseDao = new GoodsReleaseDao();
        $this->StarDataServer = (new StarDataServer);
    }

    /**
     * 通过星链返回的状态，判断商品要执行的操作
     * @param int $state
     * @return string
     */
    public function getHandleAction(int $state): string
    {
        $action =  [
            Config::STATE_UP => 'putOnTheShelf',
            Config::STATE_CHANGE => 'putChange',
            Config::STATE_DOWN => 'offOnTheShelf'
        ];
        return $action[$state] ?? 'default';
    }



    /**
     * 上架操作（新增）
     * @throws GuzzleException
     */
    public function putOnTheShelf(string $spuId, string $accessToken)
    {
        $spuDetailList = json_decode($this->StarGoodsServer->getSpuDetail(['spuIds' => $spuId])->getBody()->getContents(), true)['data'] ?? [];
        if($spuDetailList[0]) {
            $this->SyncService->syncGoods($spuDetailList, $accessToken);
        }
    }

    /**
     * 下架操作
     * @throws Exception
     */
    public function offOnTheShelf(string $spuId, string $accessToken)
    {
        /** 获取商品spuId对应的itemId TODO */
        $itemId = $this->GoodsReleaseDao->getItemId($spuId);
        /** 参数填充一下 */
        $param = [
            'item_id' => $itemId,
            /**  用户id */
            'yz_open_id' => ''
        ];
        /** 调用有赞下架接口 */
        $this->YouZanService->offTHeShelf($param,$accessToken);
    }


    /**
     * // （上架）变更操作
     * @param string $spuId
     * @param string $accessToken
     * @throws GuzzleException
     * @throws Exception
     */
    public function putChange(string $spuId, string $accessToken)
    {
        /**  获取全部商品详情 */
        $spuDetail = json_decode($this->StarGoodsServer->getSpuDetail(['spuIds' => $spuId])->getBody()->getContents(), true)['data'] ?? [];
        /** 获取星链商品数据详情 */
        $spuDetail['skuList'] = $this->StarDataServer->getAllInfoWithSpuId($spuDetail['spuId']);
        $itemId = $this->GoodsReleaseDao->getItemId($spuDetail['spuId']);
        /**  更新商品主要信息处理、上传 */
        $requestParam = $this->YouZanData->buildUpdateItem($spuDetail,$itemId);
        $this->YouZanService->update($requestParam, $accessToken);
        /**  更新商品sku信息处理、上传 */
//        $requestParam = $this->YouZanData->buildUpdateSkuItem($spuDetail);
//        $this->YouZanService->updateSku($requestParam, $accessToken);
    }

    /**  如果不是以上操作，使用改默认方法 */
    public function default(string $spuId, string $accessToken)
    {
       /**  TODO */

    }
}